package com.lin.cloud.disk.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lin.cloud.disk.common.entity.request.AccountRequestDTO;
import com.lin.cloud.disk.common.enums.EDeleteStatus;
import com.lin.cloud.disk.common.exception.BusinessException;
import com.lin.cloud.disk.entity.dto.LogicDeleteStatusDTO;
import com.lin.cloud.disk.entity.pojo.Department;
import com.lin.cloud.disk.entity.request.CountTotalSizeRequest;
import com.lin.cloud.disk.entity.request.department.CreateDepartmentRequest;
import com.lin.cloud.disk.entity.request.department.DeleteDepartmentRequest;
import com.lin.cloud.disk.entity.request.department.UpdateDepartmentRequest;
import com.lin.cloud.disk.entity.request.findDepartmentByIdRequest;
import com.lin.cloud.disk.service.IDepartmentService;
import com.lin.cloud.disk.mapper.DepartmentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Department)表服务实现类
 *
 * @author ljc
 * @since 2020-12-10 16:45:58
 */
@Service
@Slf4j
public class DepartmentServiceImpl implements IDepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> selectDepartmentByDepartmentIdList(List<Long> departmentIdList) {
        return departmentMapper.selectDepartmentByDepartmentIdList(departmentIdList);
    }

    /**
     * 创建公司部门
     * @param request
     */
    @Override
    public Long createCompanyDepartment(CreateDepartmentRequest request){
        Boolean isRootDepartment = true;
        Long parentId = request.getParentId();
        if (null != parentId){
            // 查询父部门是否存在
            Department parentDepartment = departmentMapper.selectById(parentId);
            if (null == parentDepartment){
                // 父部门不存在 则抛异常
                throw new BusinessException("新建部门指定的父部门不存在");
            }
            isRootDepartment = false;
        }
        // 创建子部门
        Department department = new Department();
        BeanUtil.copyProperties(request,department,false);
        department.setCreateBy(request.getUserDTO().getUsername());
        department.setCreateTime(new Date());
        department.setIsDelete(EDeleteStatus.UN_DELETE.getStatus());
        Long id = IdWorker.getId();
        department.setId(id);
        // 如果是根部门 则parentId == id
        if (isRootDepartment){
            department.setParentId(id);
        }else{
            // 不是根部门 parentId为传入的parentId
            department.setParentId(parentId);
        }
        // 保存部门
        departmentMapper.insert(department);
        return id;
    }


    /**
     * 删除公司部门
     * @param request
     */
    @Override
    public void deleteCompanyDepartment(DeleteDepartmentRequest request){
        LogicDeleteStatusDTO deleteStatusDTO = LogicDeleteStatusDTO.getDeleteStatusInstance(request.getDepartmentIdList(),request.getUserDTO().getUsername());
        Integer updateCount = departmentMapper.logicDeleteOrRecover(deleteStatusDTO);
        if (0 == updateCount){
            throw new BusinessException("删除公司部门失败,请公司部门是否存在");
        }
        return;
    }

    /**
     * 修改公司部门
     * @param request
     */
    @Override
    public void updateCompanyDepartment(UpdateDepartmentRequest request){
        Department department = new Department();
        department.setId(request.getDepartmentId());
        department.setUpdateBy(request.getUserDTO().getUsername());
        department.setUpdateTime(new Date());
        if (StringUtils.isNotBlank(request.getDepartmentName())){
            department.setDepartmentName(request.getDepartmentName());
        }
        if (StringUtils.isNotBlank(request.getDepartmentDesc())){
            department.setDepartmentDesc(request.getDepartmentDesc());
        }
        if (null != request.getParentId() && 0 != request.getParentId()){
            department.setParentId(request.getParentId());
        }
        // 更新
        departmentMapper.updateById(department);
    }

    /**
     * 通过ID获取部门详情
     * @param request
     * @return
     */
    @Override
    public Department findDepartmentById(findDepartmentByIdRequest request){
        return departmentMapper.selectById(request.getDepartmentId());
    }

    /**
     * 统计部门数量
     * @param request
     * @return
     */
    @Override
    public Integer countDepartmentSize(CountTotalSizeRequest request){
        return departmentMapper.countDepartmentSize(request);
    }
}